package com.examples.gateway.helper.filter;

import com.examples.gateway.common.CheckState;
import com.examples.gateway.entity.SystemPermission;
import com.examples.gateway.helper.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * 判断是否为公开访问的接口
 * @author Dai YaLu
 */
@Slf4j
public class PublicRequestFilter extends AbstractGlobalHelperFilter{
    @Override
    public Mono<Void> doFilter(ServerWebExchange exchange, GatewayFilterChain chain) {
        SystemPermission permission = exchange.getAttribute(Constants.EXAMPLES_GATEWAY_CHECK_PERMISSION);
        if (null != permission && permission.canPublicAccess()) {
            exchange.getAttributes().put(Constants.EXAMPLES_GATEWAY_CHECK_STATE, CheckState.SUCCESS_PUBLIC_ACCESS);
        }
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return 30;
    }
}
